<template>
	<app-dialog ref="dialog" width="90%" :title="title" @confirm="save">
		<view style="padding: 20rpx">
			<view class="camera">

				<view class="title">照片</view>
				<view class="box">
					<u-upload :sizeType="['compressed']" width="160" height="160" :capture="[ 'album']" :fileList="img"
						@afterRead="afterRead" @delete="del" name="1" :maxCount="1"></u-upload>
				</view>
			</view>

			<view class="camera">
				<view class="title">视频</view>
				<view class="box">
					<u-upload @delete="del" width="160" height="160" :fileList="video" :compressed="true"
						:maxDuration="60" :capture="[ 'album']" name="2" @afterRead="afterRead" accept="video"
						:maxCount="1"></u-upload>
				</view>
			</view>
		</view>

	
	</app-dialog>
</template>

<script>
	export default {
		components: {},

		data() {
			return {
				title: '上传',
				img: [],
				video: [],
				flid: ""
			};
		},

		methods: {
			/**
			 * 打开弹框
			 */
			/**
	
		 */
			async open(flid) {
				this.flid = flid;
				this.reset();

				// 打开弹框
				await this.$refs.dialog.open();
			},
			del(e) {
				if (e.name == "1") {
					this.img = [];
				} else {
					this.video = [];
				}

			},

			async afterRead(e) {
				if (e.name == "1") {
					this.img = [e.file];
				} else {
					this.video = [e.file];
				}
			},

			reset() {

			},

			/**
			 * 保存
			 */
			async save() {
				try {
				
					if (this.img.length == 0) {
						this.toast('请上传图片')
					} else if (this.video == 0) {
						this.toast('请上传视频')
					} else {
						this.showLoading('上传中')
						let imgRes = await this.$uploadFile("/app/tp/uploadMf", this.img[0].url, {
							sfar: 1,
							flid: this.flid
						})
						let imgId = imgRes.tp.id;
						let videoRes = await this.$uploadFile("/app/tp/uploadMf", this.video[0].url, {
							sfar: 1,
							flid: this.flid
						})
						let videoId = videoRes.tp.id;
						await this.$http.post('/app/tp/addMf', {
							spid: videoId,
							tpid: imgId
						})
						
						this.hideLoading('上传完成');
						this.$emit('refreshData');
						this.$refs.dialog.close();
					}



					//	this.hideLoading();

				} catch (e) {
					this.errorLoading(e);
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.camera {
		.title {
			margin-left: 20rpx;
			margin-top: 10rpx;
		}

		.box {
			padding: 0rpx 20rpx 20rpx 20rpx;
			margin-top: 20rpx;
		}
	}
</style>